<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/23
 * Time: 15:27
 */
namespace app\api\validate;

use app\lib\exception\ParemeterException;
use think\Request;
use think\Validate;

class BaseValidate extends Validate {

    /**
     * 通用验证层
     * @return bool
     * @throws ParemeterException
     */
    public function goCheck(){
        $request = Request::instance();
        $params = $request->param();
//        print_r($params);die;
        $result = $this->check($params);
        if(!$result){
            $e =  new ParemeterException([
                'msg'=>$this->getError(),
                'code'=> 400,
                'errorCode'=> 10002
            ]);
            throw $e;
        }else{
            return true;
        }
    }

    /**
     * 判断是否为正整数
     * @param $value
     *
     * @return bool
     */
    protected function isPositveInteger($value){
        if(!preg_match("/^[1-9][0-9]*$/",$value)){
            return false;
        }
        return true;
    }

    /**
     * 判断是否为空
     * @param $value
     *
     * @return bool
     */
    protected function isNotEmpty($value){
        if(empty($value)){
            return false;
        }
        return true;
    }


    protected  function isMobile($mobile){
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }

}